import api from './request'

export interface SearchHistory {
  id: number
  keyword: string
  searchType: string
  resultCount: number
  searchTime: string
}

export interface SaveSearchHistoryParams {
  userId: number
  keyword: string
  searchType?: string
  resultCount?: number
}

// 保存搜索记录
export const saveSearchHistory = async (params: SaveSearchHistoryParams) => {
  try {
    const response = await api.post('/questions/search/history', params)
    return response.data
  } catch (error) {
    console.error('保存搜索记录失败:', error)
    throw error
  }
}

// 获取搜索历史
export const getSearchHistory = async (page = 0, size = 10) => {
  try {
    const response = await api.get('/questions/search/history', {
      params: { page, size }
    })
    return response.data
  } catch (error) {
    console.error('获取搜索历史失败:', error)
    throw error
  }
}

// 获取最近搜索记录
export const getRecentSearchHistory = async () => {
  try {
    const response = await api.get('/questions/search/history/recent')
    return response.data
  } catch (error) {
    console.error('获取最近搜索记录失败:', error)
    throw error
  }
}

// 清空搜索记录
export const clearSearchHistory = async () => {
  try {
    const response = await api.delete('/questions/search/history')
    return response.data
  } catch (error) {
    console.error('清空搜索记录失败:', error)
    throw error
  }
}
